<?php

namespace app\common\model;

use think\Model;

class TaocanOrder extends Model
{
    protected $name='taocan_order';
    protected $insert = ['ctime'];
    //
    protected function setCtimeAttr()
    {
        return time();
    }
     /**
     * 订单支付成功回调
     * @param type $id
     * @param type $code
     * @return boolean|string
     */
    public function pay($id, $code)
    {
        $return_data = array(
            'status' => false,
            'msg' => '订单支付失败',
            'data' => array()
        );
        $where = [];
        $where['id'] = $id;
        $where['pay_status'] = 0;
        $taocan = $this->where($where)->find();
        if(!$taocan) {
            return $return_data;
        }
        $data['pay_status'] = 1;
        $data['payment_code'] = $code;
        if ($code == 'after_pay') {
           $data['after_status'] = 0; 
        }
        $result = $this->where('id', 'eq', $id)->update($data);
        $return_data['data'] = $result;
        if($result !== false) {
            $return_data['status'] = true;
            $return_data['msg'] = '订单支付成功';
        }
        return $return_data;
    }
    
    public function taocanCancel($id) 
    {
        if (!is_numeric($id)) {
            return [
                'status' => true,
                'data' => '',
                'msg' => 'id不能为空',
            ];
        }
        $taocan = $this->get($id);
        if(empty($taocan)) {
            return [
                'status' => true,
                'data' => '',
                'msg' => '查无此单',
            ];
        }
        if ($taocan->pay_status == '1') {
            if($taocan->payment_code != 'offline'){
                $billPaymentsModel = new BillPayments();
                $info = $billPaymentsModel->getSuccessPaymentInfo($taocan->id, 5);

                $paymentsModel = new Payments();
                $paymentInfo = $paymentsModel->getPayment($taocan->payment_code, $paymentsModel::PAYMENT_STATUS_YES);
                $conf = json_decode($paymentInfo['params'], true);

                if ($taocan->payment_code == 'balance') {
                    $balanceModel = new Balance();
                    $balanceModel->change($taocan->user_id, 2, $taocan->price, $taocan->id);
                } else if($taocan->payment_code == 'after_pay') {
                    
                } else {
                    $payment = \org\Payment::create($taocan->payment_code, $conf);       //'wechatpay' or alipa
                    $res = $payment->refund($info['trade_no'], 1);
                }
            }
        }
        if($taocan->status == 1){
            //执行加库存
            $taocan_info = json_decode($taocan->taocan, true);
            if($taocan_info){
                $stockListModel = new StockList();//StockListModel();
                foreach ($taocan_info['goods'] as &$v){
                    $stockListModel->change($taocan->user_id,$v['id'],$v['total'], 2,"",$taocan->room_bn);
                }
            }
        }
        $this->save(['pay_status' => 2],['id' => $id]);
        return [
            'status' => true,
            'data' => '',
            'msg' => '取消成功',
        ];
    }
}
